using System;

namespace Tool
{
    public static partial class ExtensionHelper
    {
        /// <summary>
        /// 返回格式为(yyyy-MM-dd HH:mm:ss)的时间字符串
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string ToString2(this DateTime dt) => dt.ToString("yyyy-MM-dd HH:mm:ss");

        /// <summary>
        /// 返回格式为(yyyy-MM-dd HH:mm:ss)的时间字符串
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string ToStringLongDate(this DateTime dt) => dt.ToString("yyyy-MM-dd HH:mm:ss");

        /// <summary>
        /// 返回格式为(yyyy-MM-dd)的时间字符串
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string ToStringDate(this DateTime dt) => dt.ToString("yyyy-MM-dd");

        /// <summary>
        /// 获取枚举类型的字符串
        /// </summary>
        /// <param name="obj">枚举对象</param>
        /// <returns>string</returns>
        public static string ToString(this System.Enum obj) => System.Enum.GetName(obj.GetType(), obj);

        /// <summary>
        /// 扩展EndTrim
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="trimstr">需要去除的字符串</param>
        /// <returns></returns>
        public static string Trim(this string str, string trimstr)
        {
            return str.Trim(trimstr.Trim().ToCharArray()); ;
        }

        #region TrimEnd

        /// <summary>
        /// 扩展EndTrim
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="trimstr">末尾需要去除的字符串</param>
        /// <returns></returns>
        public static string TrimEnd(this string str, string trimstr)
        {
            return str.TrimEnd(trimstr.Trim().ToCharArray()); ;
        }

        #endregion TrimEnd

        #region TrimStart

        /// <summary>
        /// 扩展EndTrim
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="trimstr">字符串开始需要去除的字符串</param>
        /// <returns></returns>
        public static string TrimStart(this string str, string trimstr)
        {
            return str.TrimStart(trimstr.Trim().ToCharArray());
        }

        #endregion TrimStart

        /// <summary>
        /// 检查字符串最大长度，返回指定长度的串
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <param name="maxLength">最大长度</param>
        /// <returns>返回值</returns>
        public static string Split(this string input, int maxLength)
        {
            if (!string.IsNullOrEmpty(input))
            {
                input = input.Trim();
                if (input.Length > maxLength) //按最大长度截取字符串
                {
                    input = input.Substring(0, maxLength);
                }
            }
            return input;
        }

        #region ToSBC 转全角的函数(SBC case)

        /// <summary>
        /// 转全角的函数(SBC case)
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToSBC(this string str)
        {
            string a = "";
            //半角转全角：
            char[] c = str.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 32)
                {
                    c[i] = (char)12288;
                    continue;
                }
                if (c[i] < 127)
                    c[i] = (char)(c[i] + 65248);
            }
            return new string(c);
        }

        #endregion ToSBC 转全角的函数(SBC case)

        #region ToDBC 转半角的函数(SBC case)

        /// <summary>
        ///  转半角的函数(SBC case)
        /// </summary>
        /// <param name="str">输入</param>
        /// <returns></returns>
        public static string ToDBC(this string str)
        {
            char[] c = str.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }
            return new string(c);
        }

        #endregion ToDBC 转半角的函数(SBC case)

        /// <summary>
        /// 移除字符串末尾指定字符
        /// </summary>
        /// <param name="str">需要移除的字符串</param>
        /// <param name="removeStr">移除的指定字符</param>
        /// <returns>移除后的字符串</returns>
        public static string RemoveLastStr(this string str, string removeStr)
        {
            int _finded = str.LastIndexOf(removeStr);
            if (_finded != -1)
            {
                return str.Substring(0, _finded);
            }
            return str;
        }

        /// <summary>
        /// 移除字符串开始指定字符
        /// </summary>
        /// <param name="str">需要移除的字符串</param>
        /// <param name="removeStr">移除的指定字符</param>
        /// <returns>移除后的字符串</returns>
        public static string RemoveStartStr(this string str, string removeStr)
        {
            int _finded = str.LastIndexOf(removeStr);
            if (_finded != -1)
            {
                // return str.Substring(0, _finded);
                return str.Substring(_finded, str.Length - _finded);
            }
            return str;
        }

        /// <summary>
        /// 在字符串末尾，换行添加内容
        /// </summary>
        /// <param name="Str"></param>
        /// <param name="appendstr"></param>
        public static string AppendLine(this string Str, string appendstr) => $"{Str}\r\n{appendstr}";
    }
}